/*
 * @lc app=leetcode.cn id=74 lang=java
 *
 * [74] 搜索二维矩阵
 */

// @lc code=start
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        if(m == 0 || n == 0){
            return false;
        }
        
        int left = 0, right = m * n - 1;

        while(left <= right) {
            int mid = left + (right - left) / 2;
            int value = matrix[mid / n][mid % n];
            if(value == target){
                return true;
            }else if(value < target){
                left = mid + 1;
            }else{
                right = mid - 1;
            }

        }
        return false;

    }
}
// @lc code=end

